<?php
/**
 * Author: 孙武 QQ:1228746736
 * Date: 2018/2/15
 * Time: 11:26
 */

/**
 * 审核类
 */

namespace app\home\controller;

use app\home\def\Err;
use app\home\def\Helper;
use app\home\def\Status;
use app\home\model\Resource;
use app\home\model\ResourceCategory;

class Audit extends \think\Controller
{

    private $uid;
    private $err = null;
    private $resModel = null;
    private $catModel = null;
    private $dataStatus = null;
    //允许的管理员
    private $allowUser = [
        'lbn',
        'sw',
    ];


    public function __construct()
    {
        parent::__construct();
        $this->resModel = new Resource();
        $this->catModel = new ResourceCategory();
        $this->err = new Err();
        $this->dataStatus = Status::getAllStatus();
        $this->uid = session('uid');
        if (!Helper::checkAuth($this->uid, $this->allowUser)) {
            $this->error('没有权限');
        }
    }

    /*分类开始*/
    /**
     * 添加分类|修改分类页面
     * @return \think\response\View
     */
    public function addCat($guid = '')
    {
        if ($guid) {
            $data = $this->catModel->find($guid);
            $this->assign('i', $data);

            return view('modifyCat');
        } else {
            return view();
        }
    }

    /**
     * 检查当前分类名称是否存在
     *
     * @param $name
     *
     * @return array|bool
     * 使用方法
     * //检查当前分类名称是否存在
     * if($this->checkCatNameIsExist(input('post.Name'))!==true) return
     * $this->checkCatNameIsExist(input('post.Name'));
     */
    private function checkCatNameIsExist($name)
    {
        $catName = $this->catModel->where('Name', $name)->value('Name');
        if ($catName) return $this->err->info(12001);

        return true;
    }

    /**
     * 分类列表
     */
    public function listCat()
    {
        return view();
    }

    /**
     * 添加分类API
     */
    public function addCatApi()
    {
        //检查当前分类名称是否存在
        if ($this->checkCatNameIsExist(input('post.Name')) !== true) return $this->checkCatNameIsExist(input('post.Name'));
        $data = input('post.');
        $data['Status'] = Status::$able['Status'];
        $data['PK_ResourceCategoryGuid'] = get_guid();
        $res = $this->catModel->save($data);
        if ($res > 0) {
            return $this->err->info(0);
        }

        return $this->err->info(-1);
    }

    /**
     * 删除分类api
     */
    public function delCatApi($guid = '')
    {
        return $this->err->resDelete($res = $this->catModel->where('PK_ResourceCategoryGuid', $guid)->delete());
    }

    /**
     * 修改分类接口
     */
    public function modifyCatApi()
    {
        //检查当前分类名称是否存在
        $guid = input('post.guid');
        $catName = input('post.Name');
        $dbCatNameEqualNowCount = $this->catModel->where('PK_ResourceCategoryGuid', 'neq', $guid)->where('Name', $catName)->count('Name');
        if ($dbCatNameEqualNowCount > 0) return $this->err->info(12001);
        return $this->err->resUpdata($this->catModel->allowField(true)->save(input('post.'), ["PK_ResourceCategoryGuid" => input('post.guid')]));
    }

    /**
     * 显示分类api
     *
     * @param $page  int 页数
     * @param $limit int 每页显示的数量
     *
     * @return array
     */
    public function listCatApi($page, $limit)
    {
        return $this->err->layuiTable(0, $this->catModel->count(), $this->catModel->page($page, $limit)->select());
    }


    /**
     * 更新启用/禁用
     */
    public function ableOrDisableCatApi($guid = '')
    {
        $staus = $this->catModel->where('PK_ResourceCategoryGuid', $guid)->value('Status');
        if ($staus === Status::$able['Status']) {
            return $this->err->resUpdata($this->catModel->save(Status::$disable, ['PK_ResourceCategoryGuid' => $guid]));
        } else {
            return $this->err->resUpdata($this->catModel->save(Status::$able, ['PK_ResourceCategoryGuid' => $guid]));
        }
    }


    /**
     * 禁用分类
     */
    public function disableCatApi($guid = '')
    {
        return $this->err->resUpdata($this->catModel->save(Status::$disable, ['PK_ResourceCategoryGuid' => $guid]));
    }

    /**
     * 启用分类
     */
    public function ableCatApi($guid = '')
    {
        return $this->err->resUpdata($this->catModel->save(Status::$able, ['PK_ResourceCategoryGuid' => $guid]));
    }
    /*分类结束*/

    /**
     * 资源列表|资源详情
     */
    public function listRes($guid = '')
    {
        if ($guid) {
            $data = $this->resModel->getResource($guid);
            $this->assign('i', $data);

            return view('modifyRes');
        } else {
            return view();
        }

    }

    /**
     * 资源列表|资源详情
     */
    public function listResAll($guid = '')
    {

        return view();


    }

    public function listResAllApi()
    {

        $data = $this->resModel->with('category')->select();

        return $this->err->layuiTable(0, $this->resModel->count(), $data);
    }

    /**
     * 详情 || 修改
     *
     * @param string $guid
     *
     * @return \think\response\View
     */
    public function detailRes($guid = '')
    {
        $this->assign('i', $this->resModel->find($guid));

        return view();
    }

    public function listResApi()
    {

        $data = $this->resModel->with('category')->where(Status::status('notPass'))->select();

        return $this->err->layuiTable(0, $this->resModel->where(Status::status('notPass'))->count(), $data);
    }


    public function deleteResApi($guid = '')
    {
        $guid = trim($guid);
        return $this->err->resUpdata($this->resModel->save(Status::$delete, ['PK_ResourceGuid' => $guid]));
    }

    public function recoverResApi($guid = '')
    {
        $guid = trim($guid);
        return $this->err->resUpdata($this->resModel->save(Status::$pass, ['PK_ResourceGuid' => $guid]));
    }

    public function passRes($guid = '')
    {
        return $this->err->resUpdata($this->resModel->save(Status::status('pass'), ['PK_ResourceGuid' => $guid]));
    }


    private function getGuid()
    {
        $guid = input('param.guid');

        return $guid;
    }

    public function index()
    {
        return view();
    }

    /**
     * 获取允许的用户
     * @return array
     */
    public function getAllowUsr()
    {
        return $this->allowUser;
    }


    public function dbBackup()
    {
        $backUpFile = date('Ymd') . '.sql';
        $res = Helper::mysqlBackup($backUpFile);
        return $this->err->infoWithErrmsg($res['errno'],$res['errmsg']);

    }
}